package com.zfzs.post.modules.address.repository;

import com.zfzs.post.modules.address.domain.AgentArea;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
* @author ck
* @date 2019-09-09
*/
public interface AgentAreaRepository extends JpaRepository<AgentArea, Long>, JpaSpecificationExecutor {

    /**
    * 删除
    **/
    @Modifying
    @Query(value = "update agent_area set is_del = 1,update_time = now() where area_id = ?1",nativeQuery = true)
    void deleteAreaById(Long areaId);

    /**
     * 取消所有默认
     **/
    @Modifying
    @Query(value = "update agent_area set is_default = 0,update_time = now() where agent_id = ?1",nativeQuery = true)
    void cancelDefaultByAgentId(Long agentId);

    /**
     * 设置默认
     **/
    @Modifying
    @Query(value = "update agent_area set is_default = 1,update_time = now() where area_id = ?1",nativeQuery = true)
    void defaultAreaById(Long areaId);

    /**
     * 取消默认
     */
    @Modifying
    @Query(value = "update agent_area set is_default = 0,update_time = now() where area_id = ?1",nativeQuery = true)
    void cancelDefaultById(Long areaId);

    /**
     * 根据代理商个查询
     */
    @Query(value = "select * from agent_area where agent_id = ?1 and is_del = 0 ORDER BY is_default DESC",nativeQuery = true)
    List<AgentArea> queryAreaList(Long agentId);

    @Query(value = "select * from agent_area where agent_id = :agentId and is_del = 0 and is_default=1",nativeQuery = true)
    AgentArea findDefaultArea(Long agentId);

}